#!/usr/bin/env python
#
#       listingdefinitions.py
#
#       Copyright 2009 karthikeyan S. <kaarthikeyapreyan@gmail.com>
#
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

import wx

import pytestgenerator
import accessories

class ListEvents:

    def __init__(self,BaseFrame):

        self.uimod=None
        self.Inst=BaseFrame
        self.IDMAP={601:"class",602:"method",603:"function"}

    def ischecked(self,ID):
        """
        Verify and return the checked Items
        """
        checklist=[]
        for comp in ID:
            if self.Inst.list.IsChecked(id=comp):
                checklist.append(comp)
        return checklist

    def listitem(self,event):
        """
        Event to List Testcases
        """
        self.Inst.avail.DeleteAllItems()
        self.Inst.selc.DeleteAllItems()
        self.uimod=self.Inst.modname.GetValue()
        if len(self.uimod) > 0 and \
            pytestgenerator.TestGenerator().importcheck(self.uimod)[0]:
            elements=self.ischecked([601,602,603])
            self.listitemctl(elements)
        else :
            accessories.infobox(self.Inst,"Please Enter a valid Package Name",\
                "PyTestGenerator Error",wx.ICON_ERROR)

    def listitemctl(self,elements):
        """
        Select the type of listing
        """
        if len(elements) :
            self.listspecific(elements)
        else :
            self.listall()

    def listall(self):
        """
        List all entities
        """
        mod_dir=[]
        addlines=0
        for entity in pytestgenerator.analyze.analyzefunction(self.uimod):
            mod_dir.append([entity,'FUNCTION'])
        for entity in pytestgenerator.analyze.analyzeclass(self.uimod):
            mod_dir.append([entity,'CLASS'])

        for index,entity in enumerate(mod_dir):
            index=self.Inst.avail.GetItemCount()
            self.Inst.avail.InsertStringItem(index,"%d"%(index+1))
            self.Inst.avail.SetItemBackgroundColour(index, "#E6F1F5")
            if entity[1] == 'FUNCTION':
                self.Inst.avail.SetStringItem(index,1,entity[0])
                self.Inst.avail.SetStringItem(index,3,entity[1])
            elif entity[1] == 'CLASS':
                self.Inst.avail.SetStringItem(index,1,entity[0])
                self.Inst.avail.SetStringItem(index,3,entity[1])
                methods=pytestgenerator.analyze.getclassmethods\
                    (self.uimod,entity[0])
                for subindex,item in enumerate(methods):
                    new_index=index+(subindex+1)
                    self.Inst.avail.InsertStringItem\
                        (new_index,"%d"%(new_index+1))
                    self.Inst.avail.SetStringItem(new_index,2,item)
                    self.Inst.avail.SetStringItem(new_index,3,'METHOD')
                    self.Inst.avail.SetItemBackgroundColour\
                        (new_index,"#E6F1F5")

    def listspecific(self,elements):
        """
        List the checked entities only
        """
        req=0
        for mindex,entity in enumerate(elements):
            temp_list=[]
            if entity == 601 :
                temp_list.extend\
                    (pytestgenerator.analyze.analyzeclass(self.uimod))
                if (601 in elements) and (602 in elements):
                    temp_list=self._checkmethavail(temp_list)
            elif entity == 602:
                meth_temp=pytestgenerator.analyze.analyzemethod(self.uimod)
                map(lambda idx: idx.append('method'),meth_temp)
                temp_list.extend(meth_temp)
            elif entity == 603:
                temp_list.extend\
                    (pytestgenerator.analyze.analyzefunction(self.uimod))
            addlines=len(temp_list)
            if entity == 602:
                cc=accessories.selcolour(self.IDMAP[entity])
                temp_list=self.custgrouping(temp_list)
                addsubl=0
                class_add=len(temp_list) #For class specification in methods
                for sindex,subentity in enumerate(temp_list):
                    class_=subentity.pop(0)
                    sindex=sindex+addsubl
                    self.Inst.avail.InsertStringItem\
                        (sindex+req,"%d"%(sindex+req+1))
                    self.Inst.avail.SetStringItem(sindex+req,1,class_)
                    self.Inst.avail.SetItemBackgroundColour(sindex+req,cc)
                    self.Inst.avail.SetStringItem(sindex+req,3,'CLASS')
                    for subindex,meths in enumerate(subentity):
                        new_index=sindex+(subindex+1)
                        self.Inst.avail.InsertStringItem\
                            (new_index+req, "%d"%(new_index+req+1))
                        self.Inst.avail.SetStringItem(new_index+req,2,meths)
                        self.Inst.avail.SetItemBackgroundColour\
                            (new_index+req,cc)
                        self.Inst.avail.SetStringItem\
                            (new_index+req,3,self.IDMAP[entity].upper())
                    addsubl=len(subentity)+addsubl
            else:
                class_add=0 #Explict for methods
                for index,value in enumerate(temp_list):
                    self.Inst.avail.InsertStringItem\
                        (index+req, "%d"%(index+req+1))
                    self.Inst.avail.SetStringItem(index+req,1,value)
                    self.Inst.avail.SetStringItem\
                        (index+req,3,self.IDMAP[entity].upper())
                    cc=accessories.selcolour(self.IDMAP[entity])
                    self.Inst.avail.SetItemBackgroundColour(index+req,cc)
            req=addlines+req+class_add

    def custgrouping(self,elements):
        """
        If [[m1,c1],[m2,c1],[m1,c2]]
        Return [[c1,m1,m2],[c2,m2]]
        """
        clist=[]
        temp=[]
        mastent=[]
        for entity in elements:
            if not entity[1] in clist:
                clist.append(entity[1])
        for element in clist:
            temp.append(element)
            for entity in elements:
                if entity[1] == element:
                    temp.append(entity[0])
            mastent.append(temp)
            temp=[]
        return mastent

    def _checkmethavail(self,clist):
        ctemp=[]
        for class_ in clist:
            if len(pytestgenerator.analyze.getclassmethods\
                (self.uimod,class_)) >= 1:
                pass
            else:
                ctemp.append(class_)
        return ctemp
